﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PracticeAzure.Website.Shared;
using System.Web.Security;

namespace PracticeAzure.Website.Controllers
{
    public class MemberController : Controller
    {
        [HttpGet]
        public ActionResult LogOn()
        {
            return View();
        }

        [HttpPost]
        public ActionResult LogOn(string email, string password, bool rememberMe, string returnUrl)
        {
            var encryptedEmail = email.CalculateMD5();
            var encryptedPassword = password.CalculateMD5();
            if (string.Compare(encryptedEmail, GlobalConfiguration.AccountName, true) == 0 &&
                string.Compare(encryptedPassword, GlobalConfiguration.AccountKey, true) == 0)
            {
                FormsAuthentication.SetAuthCookie(email, rememberMe);
                if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                    && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                {
                    return Redirect(returnUrl);
                }
                else
                {
                    return RedirectToAction("Index", "Home");
                }
            }
            else
            {
                ModelState.AddModelError("", "错误的用户名或密码。");
                return LogOn();
            }
        }

        [HttpGet]
        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", "Home", null);
        }
    }
}
